add_library(ETHERNET_FILES STATIC)

target_sources(ETHERNET_FILES PUBLIC
    ${WIZNET_DIR}/Ethernet/socket.c
    ${WIZNET_DIR}/Ethernet/wizchip_conf.c
)
# Contains the search path to the library of W5100S.
# Compile selection based on whether the chip used is W5500 or W5100S，ON(1) :W5500, OFF(0):W5100S.
set(COMPILE_SEL OFF)

if (COMPILE_SEL)
        target_include_directories(ETHERNET_FILES INTERFACE
                ${WIZNET_DIR}/Ethernet
                ${WIZNET_DIR}/Ethernet/W5500
        )

        target_link_libraries(ETHERNET_FILES PUBLIC
                W5500_FILES
        )   
else()
        target_include_directories(ETHERNET_FILES INTERFACE
                ${WIZNET_DIR}/Ethernet
                ${WIZNET_DIR}/Ethernet/W5100S
        )

        target_link_libraries(ETHERNET_FILES PUBLIC
                W5100S_FILES
        )
endif()

# W5100S related library files
add_library(W5100S_FILES STATIC)

target_sources(W5100S_FILES PUBLIC
        ${WIZNET_DIR}/Ethernet/W5100S/w5100s.c
)

target_include_directories(W5100S_FILES INTERFACE
        ${WIZNET_DIR}/Ethernet
        ${WIZNET_DIR}/Ethernet/W5100S
)

target_link_libraries(W5100S_FILES PRIVATE
        ETHERNET_FILES
)
# W5500 related library files
add_library(W5500_FILES STATIC)

target_sources(W5500_FILES PUBLIC
        ${WIZNET_DIR}/Ethernet/W5500/w5500.c
)

target_include_directories(W5500_FILES INTERFACE
        ${WIZNET_DIR}/Ethernet
        ${WIZNET_DIR}/Ethernet/W5500
)

target_link_libraries(W5500_FILES PRIVATE
        ETHERNET_FILES
)
# DHCP related library files
add_library(DHCP_FILES STATIC)
target_sources(DHCP_FILES PUBLIC
        ${WIZNET_DIR}/Internet/DHCP/dhcp.c
)
target_include_directories(DHCP_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/DHCP
)
target_link_libraries(DHCP_FILES PRIVATE
        ETHERNET_FILES
)
# DNS related library files
add_library(DNS_FILES STATIC)
target_sources(DNS_FILES PUBLIC
        ${WIZNET_DIR}/Internet/DNS/dns.c
)
target_include_directories(DNS_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/DNS
)
target_link_libraries(DNS_FILES PRIVATE
        ETHERNET_FILES
)

# application example related library files
add_library(APP_FILES STATIC)
target_sources(APP_FILES PUBLIC
        ${WIZNET_DIR}/Application/loopback/loopback.c
)
target_include_directories(APP_FILES INTERFACE
        ${WIZNET_DIR}/Application
        ${WIZNET_DIR}/Application/loopback
)
target_link_libraries(APP_FILES PRIVATE
        ETHERNET_FILES
)

# multicast related library files
add_library(MULTI_FILES STATIC)
target_sources(MULTI_FILES PUBLIC
        ${WIZNET_DIR}/Application/multicast/multicast.c
)
target_include_directories(MULTI_FILES INTERFACE
        ${WIZNET_DIR}/Application
        ${WIZNET_DIR}/Application/multicast
)
target_link_libraries(MULTI_FILES PRIVATE
        ETHERNET_FILES
)


# SNTP related library files
add_library(SNTP_FILES STATIC)
target_sources(SNTP_FILES PUBLIC
        ${WIZNET_DIR}/Internet/SNTP/sntp.c
)
target_include_directories(SNTP_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/sntp
)
target_link_libraries(SNTP_FILES PRIVATE
        ETHERNET_FILES
)


# MQTT related library files
add_library(MQTT_FILES STATIC)
target_sources(MQTT_FILES PUBLIC
        ${WIZNET_DIR}/Internet/MQTT/mqtt_interface.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTClient.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTConnect.h
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTConnectClient.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTConnectServer.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTDeserializePublish.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTFormat.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTPacket.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTPublish.h
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTSerializePublish.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTSubscribe.h
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTSubscribeClient.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTSubscribeServer.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribe.h
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src/StackTrace.h
)
target_include_directories(MQTT_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/MQTT
        ${WIZNET_DIR}/Internet/MQTT/MQTTPacket/src
)
target_link_libraries(MQTT_FILES PRIVATE
        ETHERNET_FILES
)


# FTP Client related library files
add_library(FTPC_FILES STATIC)
target_sources(FTPC_FILES PUBLIC
        ${WIZNET_DIR}/Internet/FTPClient/ftpc.c
)
target_include_directories(FTPC_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/FTPClient        
)
target_link_libraries(FTPC_FILES PRIVATE
        ETHERNET_FILES
        pico_stdlib
)

#  HTTP Server related library files
add_library(HTTPS_FILES STATIC)
target_sources(HTTPS_FILES PUBLIC
        ${WIZNET_DIR}/Internet/httpServer/httpParser.c
        ${WIZNET_DIR}/Internet/httpServer/httpParser.h
        ${WIZNET_DIR}/Internet/httpServer/httpServer.c
        ${WIZNET_DIR}/Internet/httpServer/httpServer.h
        ${WIZNET_DIR}/Internet/httpServer/httpUtil.c
        ${WIZNET_DIR}/Internet/httpServer/httpUtil.c
        
)
target_include_directories(HTTPS_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/httpServer 
)
target_link_libraries(HTTPS_FILES PRIVATE
        ETHERNET_FILES
        pico_stdlib
)


# TFTP Client related library files
add_library(TFTP_FILES STATIC)
target_sources(TFTP_FILES PUBLIC
        ${WIZNET_DIR}/Internet/TFTP/netutil.c
        ${WIZNET_DIR}/Internet/TFTP/tftp.c
)
target_include_directories(TFTP_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/TFTP        
)
target_link_libraries(TFTP_FILES PRIVATE
        ETHERNET_FILES
        pico_stdlib
)


# SNMP related library files
add_library(SNMP_FILES STATIC)
target_sources(SNMP_FILES PUBLIC
        ${WIZNET_DIR}/Internet/SNMP/snmp.c
        ${WIZNET_DIR}/Internet/SNMP/snmp_custom.c
)
target_include_directories(SNMP_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/SNMP        
)
target_link_libraries(SNMP_FILES PRIVATE
        ETHERNET_FILES
        pico_stdlib
)


# FTP Server related library files
add_library(FTPS_FILES STATIC)
target_sources(FTPS_FILES PUBLIC
        ${WIZNET_DIR}/Internet/FTPServer/ftpd.c
        ${WIZNET_DIR}/Internet/FTPServer/stdio_private.h
)
target_include_directories(FTPS_FILES INTERFACE
        ${WIZNET_DIR}/Internet
        ${WIZNET_DIR}/Internet/FTPServer        
)
target_link_libraries(FTPS_FILES PRIVATE
        ETHERNET_FILES
        pico_stdlib
)